package com.zghhost.codedemo.mq.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

public class AmqpConsumerTest {
    public static void main(String[] args)throws Exception{
        Channel channel = ConnectionFactoryTest.getNewChannel();
        boolean autoAck = false;
        channel.basicConsume(ConnectionFactoryTest.TEST_Q_NAME, autoAck, "myConsumerTag",
            new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag,
                                           Envelope envelope,
                                           AMQP.BasicProperties properties,
                                           byte[] body)
                        throws IOException{
                    String msg = new String(body);
                    System.out.println(msg);
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
                    long deliveryTag = envelope.getDeliveryTag();
                    // (process the message components here ...)
                    channel.basicAck(deliveryTag, false);
                }
            });
    }
}
